// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var leftMenu = require("../leftMenu/leftMenu");
	var footer = require("../footer/footer_main");
	var css_loanNoticeList = require("./loanNoticeList.css");
	var dialog = require("dialog");
	var databind = require("databind");
	var tools = require("tools");
	var validate = require("validate");
	var constant = require("constant");
	var pageIndex = 1,pageSize = 10,totalPageCount = 1,totalRecordCount = 0; //分页初始化
	var left;
	var inventoryId;
	var init = function() {
		footer.init();
		$("#footer").removeClass("none");
		leftMenu.init(6);
		topMenu.init(0);
		$(".footer").removeClass("none");
		$(".table-section").removeClass("none");
		$("#leftMenu7").find(".leftMenu-item").removeClass("leftMenu-active");
		$("#leftMenu7 .leftMenu li:first-child").addClass("leftMenu-open");
		$(".financeUnloanA").parent().addClass("leftMenu-active");
		router.init();
		inquireLoanNoticePageList();
		initEvent();

	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		
		$("#loanNoticeSearch").on("click", function() {
			pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化
			inquireLoanNoticePageList();
		});
	};

	var inquireLoanNoticePageList = function() {
		var productType = $("#loanNoticeList select[name=productType]").val();
		var beginDate = $("#beginDate").val();
		var endDate = $("#endDate").val();
		var enterpriseName = $("#enterpriseName").val();
		var financeNumber = $("#financeNumber").val();

		base.fnAjax({
			action: "inquireLoanNoticePageList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				enterpriseName: enterpriseName,
				bulkstockName: $("#bulkstockName").val(),
				productType: productType,
				beginDate: beginDate,
				endDate: endDate,
				financeNumber: financeNumber,
				loanState: 1
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("formatCurrency", tools.formatCurrency);
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#bulkstockInventoryTpl").html();
					var html = juicer(tpl, data);
					$("#noticeListBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#loanNoticeList .pagerCon"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireLoanNoticePageList();
						});
//					$("#noticeListBody").unbind("click").click(function() {
					$("#noticeListBody .detail").unbind("click").click(function() {
						var loanNoticeId = [];
						var $this = $(this).parents("tr.tr_style");
						loanNoticeId.push($this.attr("loanNoticeId"));
						router.route("financing/loanNotice.html?loanNoticeId=" + loanNoticeId);
					});
					$("#loanNoticeList .approve").unbind("click").click(function() {
						router.route("financing/loanNotice.html?loanNoticeId=" + $(this).parents("tr").attr("loannoticeid")+"&type=approve");
					});
					
					$("#loanNoticeList .detail").unbind("click").click(function() {
						router.route("financing/loanNotice.html?loanNoticeId=" + $(this).parents("tr").attr("loanNoticeId") + "&financeId=" + $(this).parents("tr").attr("financeId"));
					});
					
					$("#loanNoticeList .close").unbind("click").click(function(){
						var $this = $(this);
						layer.confirm('是否确认关闭该条记录？', {
						  btn: ['确定','取消'] //按钮
						}, function(){
						  opposeLoanNotice($this.parents("tr").attr("loanNoticeId"));
						}, function(){
						});
					})
					tools.changeWarningColor($("#noticeListBody"), "loanAuditCreateTime");
//					tools.changeWarningColor($("#loanNoticeList #noticeListBody"), "loanAuditCreateTime");
				} else {
					base.alert(3, "信息获取失败");
				}
			}
		});
	};
	
	var opposeLoanNotice = function(loanNoticeId){
		base.fnAjax({
			action: "opposeLoanNotice.json",
			oData: {
				loanNoticeId:loanNoticeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if (data.status == 200) {
						layer.msg('关闭成功！', {icon: 1});
						location.reload();
					} else {
						layer.msg(data.message, {icon: 2});
					}
				} else {
					base.alert(3, "信息获取失败");
				}
			}
		});
	};
	
	return {
		init: init
	};
});